package com.bonc.config;

import com.bonc.modules.sys.oauth2.OAuth2Filter;
import com.bonc.modules.sys.oauth2.OAuth2Realm;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Shiro配置
 * @author jinlong
 */
@Configuration
public class ShiroConfig {

    @Bean("securityManager")
    public SecurityManager securityManager(OAuth2Realm oAuth2Realm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(oAuth2Realm);
        securityManager.setRememberMeManager(null);
        return securityManager;
    }

    @Bean("shiroFilter")
    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        shiroFilter.setSecurityManager(securityManager);

        //oauth过滤
        Map<String, Filter> filters = new HashMap<>(16);
        filters.put("oauth2", new OAuth2Filter());
        shiroFilter.setFilters(filters);

        Map<String, String> filterMap = new LinkedHashMap<>();
        filterMap.put("/webjars/**", "anon");
        filterMap.put("/druid/**", "anon");
        filterMap.put("/app/**", "anon");
        filterMap.put("/sys/login", "anon");
        filterMap.put("/sys/singleLogin", "anon");
        filterMap.put("/swagger/**", "anon");
        filterMap.put("/v2/api-docs", "anon");
        filterMap.put("/swagger-ui.html", "anon");
        filterMap.put("/swagger-resources/**", "anon");
        filterMap.put("/captcha.jpg", "anon");
        filterMap.put("/oa/**", "anon");
        filterMap.put("/file/fileDownload", "anon");
        filterMap.put("/rpt/**", "anon");
        filterMap.put("/survey/**", "anon");

        filterMap.put("/business/tbworkoederdistribution/DispatchCSS", "anon");
        filterMap.put("/business/tbworkoederdistribution/DispatchCSService", "anon");
        filterMap.put("/business/tbworkoederdistribution/putsftpInfoService", "anon");
        filterMap.put("/business/tbworkoederdistribution/getsftpInfoService", "anon");
        filterMap.put("/business/tbworkoederdistribution/getTokenService", "anon");
        filterMap.put("/business/tbworkorderurge/UrgeCSS", "anon");
        filterMap.put("/business/tbworkorderurge/UrgeCSService", "anon");
        filterMap.put("/business/tbworkorderreprocee/ReprocessCSS", "anon");
        filterMap.put("/business/tbworkorderreprocee/ReprocessCSService", "anon");
        filterMap.put("/business/tbworkorderarchive/StatementCSS", "anon");
        filterMap.put("/business/tbworkorderarchive/StatementCSService", "anon");
        filterMap.put("/business/tbworkordercancel/WithdrawCSS", "anon");
        filterMap.put("/business/tbworkordercancel/WithdrawCSService", "anon");
        filterMap.put("/business/tbworkorderinfo/QueryCSS", "anon");
        filterMap.put("/business/tbworkorderinfo/QueryCSService", "anon");
        filterMap.put("/business/tbworkorderreply/ReplyCSS", "anon");
        filterMap.put("/business/tbworkorderreply/ReplyCSService", "anon");
        filterMap.put("/rpt/arc/playAudio", "anon");

        filterMap.put("/workorderForProvince/ReturnWorkOrder", "anon");
        filterMap.put("/workorderForProvince/workOrder", "anon");
        filterMap.put("/workorderForProvince/queryCSS", "anon");
        filterMap.put("/ssoOa/oaLogin", "anon");
        filterMap.put("/**", "oauth2");
        shiroFilter.setFilterChainDefinitionMap(filterMap);

        return shiroFilter;
    }

    @Bean("lifecycleBeanPostProcessor")
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }

}
